package algorithm.poj;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * id: 1006
 * @author wong.tong@gmail.com
 *
 */
public class Biorhythms {

	public static void main(String[] args) throws Exception {
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

        String line;
        String[] tmp;

        int cnt = 0;
        int days = 0;
        String s = "";
        
        line = stdin.readLine();
        while (line != null && line.trim().length() > 0 && !"-1 -1 -1 -1".equals(line)) {
        	cnt ++;
        	tmp = line.split("\\s+");
        	days = days(Integer.valueOf(tmp[0]), Integer.valueOf(tmp[1]), 
        			Integer.valueOf(tmp[2]), Integer.valueOf(tmp[3]));
        	
        	s += "Case " + cnt + ": the next triple peak occurs in " + days + " days.\n";
        	line = stdin.readLine();
        }
        System.out.println(s);
	}
	
	private static int days(int p, int e, int i, int d) {

		int days = d;
		int delta = 1;
		int pp, ee, ii;
		while (true) {
			pp = (days-p)%23;
			ee = (days-e)%28;
			ii = (days-i)%33;
			if (pp == 0 && ee== 0 && ii== 0) {
				break;
			} else {
				if (pp != 0 && ee != 0 && ii != 0) {
					delta = 1;					
				} else if (pp == 0 && ee != 0 && ii != 0) {
					delta = 23;
				} else if (pp != 0 && ee == 0 && ii != 0) {
					delta = 28;
				} else if (pp != 0 && ee != 0 && ii == 0) {
					delta = 33;
				} else if (pp == 0 && ee == 0 && ii != 0) {
					delta = 644; //23*28
				} else if (pp != 0 && ee == 0 && ii == 0) {
					delta = 924; //28*33
				} else if (pp == 0 && ee != 0 && ii == 0) {
					delta = 759; //23*33
				}
				days += delta;
				continue;
			}
		}
		if (days == d) {
			return 21252;
		} else {
			return days-d;
		}
	}
}
